package com.example.smartcity.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity.R
import com.example.smartcity.app.App
import com.example.smartcity.http.ThemeBean

/**
 * @Author: Tailyou
 * @CreateDate: 2021/10/7 19:11
 * @Email: tailyou@163.com
 * @Description: RecycleView数据适配器（外卖分类）
 */
class TOFThemeGridAdapter(var datas: List<ThemeBean>, var listener: OnItemClickListener?) :
    RecyclerView.Adapter<TOFThemeGridAdapter.MViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MViewHolder {
        var view = LayoutInflater.from(parent.context)
            .inflate(R.layout.layout_item_grid_tof_theme_entry, null)
        return MViewHolder(view)
    }

    override fun onBindViewHolder(holder: MViewHolder, position: Int) {
        holder.textView.text = datas[position].themeName
        App.fetchRCImage(
            datas[position].imgUrl,
            holder.imageView,
            error = R.mipmap.ic_more_service
        )
        if (listener != null) {
            holder.itemView.setOnClickListener {
                listener?.onClick(position)
            }
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    class MViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var imageView: ImageView = itemView.findViewById(R.id.grid_image)
        var textView: TextView = itemView.findViewById(R.id.grid_name)
    }

    interface OnItemClickListener {
        fun onClick(position: Int)
    }
}